{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4bca8488",
   "metadata": {},
   "source": [
    "-------\n",
    "## week12\n",
    "* 本周主要内容：语音识别\n",
    "* 优化ASR(自动语音识别)机器人\n",
    "* 连接图灵机器人\n",
    "* 语音合成\n",
    "<br/>\n",
    "<br/>\n",
    "\n",
    "-------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0849c6b0",
   "metadata": {},
   "source": [
    "## 优化ASR(自动语音识别）机器人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ecf32a33",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 百度密钥\n",
    "API_KEY = 'WwwxbHduUMDXrPf2VOVNLjIp'\n",
    "SECRET_KEY = 'ERrGGrpX8YkTtAS9TuNz0KNSNmNntxU1'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "36108291",
   "metadata": {},
   "outputs": [],
   "source": [
    "from bdasr import fetch_token,asr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "84a63f0a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SUCCESS WITH TOKEN: 24.b7bc7d8b5ff272a91e93970a31dd923a.2592000.1655908473.282335-26194928 ; EXPIRES IN SECONDS: 2592000\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'24.b7bc7d8b5ff272a91e93970a31dd923a.2592000.1655908473.282335-26194928'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xf_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "xf_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8a36b9e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "17671be0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.733758\n",
      "{\"corpus_no\":\"7100940221293272545\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"你好。\"],\"sn\":\"68430694771653316482\"}\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'{\"corpus_no\":\"7100940221293272545\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"你好。\"],\"sn\":\"68430694771653316482\"}\\n'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "asr(xf_token,\"1.wav\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ae50c7a",
   "metadata": {},
   "source": [
    "## 连接图灵机器人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7fc849d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "b40f92f9",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'json' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-15-39d09a7438be>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     15\u001b[0m }\n\u001b[0;32m     16\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 17\u001b[1;33m \u001b[0mres\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mrequests\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpost\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mTL_URL\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mjson\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdumps\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpayload\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'json' is not defined"
     ]
    }
   ],
   "source": [
    "TL_KEY = '44f662cceda24605891834e31862e9f8'\n",
    "TL_URL = \"http://openapi.turingapi.com/openapi/api/v2\"\n",
    "\n",
    "payload={\n",
    "    \"reqType\":0,\n",
    "    \"perception\": {\n",
    "        \"inputText\": {\n",
    "            \"text\": \"苹果英语怎么说\"\n",
    "        },\n",
    "    },\n",
    "    \"userInfo\": {\n",
    "        \"apiKey\": \"\",\n",
    "        \"userId\": \"\"\n",
    "    }\n",
    "}\n",
    "\n",
    "res = requests.post(TL_URL,data=json.dumps(payload))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37d3e574",
   "metadata": {},
   "source": [
    "## 对话机器人的流程\n",
    "* 1.录制音频\n",
    "* 2.语音识别--（百度ASR接口）--音频转文本信息\n",
    "* 3.图灵API（进行文字输入和文字回复）\n",
    "* 4.(待做)语音合成(文字信息输出音频)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "c8ff06a0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SUCCESS WITH TOKEN: 24.c0cf978a0f3429e4686f45d14551615c.2592000.1655908843.282335-26194928 ; EXPIRES IN SECONDS: 2592000\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'TL_API' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-17-22036d020ac6>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     12\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     13\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 14\u001b[1;33m \u001b[0mTL_API\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mTL_KEY\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0masr_output\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'TL_API' is not defined"
     ]
    }
   ],
   "source": [
    "# 1. 录制音频\n",
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone()as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\",\"wb\")as f:\n",
    "    f.write(audio.get_wav_data(convert_rate=16000))\n",
    "\n",
    "\n",
    "xf_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "\n",
    "\n",
    "TL_API(TL_KEY,asr_output)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d9fa549",
   "metadata": {},
   "source": [
    "## 语音合成测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "5d572e26",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fetch token begin\n",
      "{\"refresh_token\":\"25.d0e1082017f1a73986c95525fea7b158.315360000.1968677026.282335-26194928\",\"expires_in\":2592000,\"session_key\":\"9mzdDcYy+e1w06s++CtbfBMo9Tev4Bxpunbp07IGv3+frIVApeMk7ymx+MM05rVyIf\\/rjjffjQqT7toCDFtbHakYNgD9yg==\",\"access_token\":\"24.6059fa193187cab50b2051958852235c.2592000.1655909026.282335-26194928\",\"scope\":\"audio_voice_assistant_get brain_enhanced_asr audio_tts_post brain_speech_realtime public brain_all_scope brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\\u6743\\u9650 vis-classify_flower lpq_\\u5f00\\u653e cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_\\u5f00\\u653eScope vis-ocr_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 idl-video_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx\",\"session_secret\":\"7cce0e8017ee789114a5f413ef0e8569\"}\n",
      "\n",
      "{'refresh_token': '25.d0e1082017f1a73986c95525fea7b158.315360000.1968677026.282335-26194928', 'expires_in': 2592000, 'session_key': '9mzdDcYy+e1w06s++CtbfBMo9Tev4Bxpunbp07IGv3+frIVApeMk7ymx+MM05rVyIf/rjjffjQqT7toCDFtbHakYNgD9yg==', 'access_token': '24.6059fa193187cab50b2051958852235c.2592000.1655909026.282335-26194928', 'scope': 'audio_voice_assistant_get brain_enhanced_asr audio_tts_post brain_speech_realtime public brain_all_scope brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx', 'session_secret': '7cce0e8017ee789114a5f413ef0e8569'}\n",
      "SUCCESS WITH TOKEN: 24.6059fa193187cab50b2051958852235c.2592000.1655909026.282335-26194928 ; EXPIRES IN SECONDS: 2592000\n",
      "%E6%AC%A2%E8%BF%8E%E4%BD%BF%E7%94%A8%E7%99%BE%E5%BA%A6%E8%AF%AD%E9%9F%B3%E5%90%88%E6%88%90%E3%80%82\n",
      "test on Web Browserhttp://tsn.baidu.com/text2audio?tok=24.6059fa193187cab50b2051958852235c.2592000.1655909026.282335-26194928&tex=%25E6%25AC%25A2%25E8%25BF%258E%25E4%25BD%25BF%25E7%2594%25A8%25E7%2599%25BE%25E5%25BA%25A6%25E8%25AF%25AD%25E9%259F%25B3%25E5%2590%2588%25E6%2588%2590%25E3%2580%2582&per=4&spd=5&pit=5&vol=5&aue=3&cuid=123456PYTHON&lan=zh&ctp=1\n",
      "tts api  error:{\"err_detail\":\"16: Open api characters limit reached\",\"err_msg\":\"16: Open api characters limit reached\",\"err_no\":502,\"err_subcode\":16,\"tts_logid\":1848638486}\n",
      "\n",
      "result saved as :error.txt\n"
     ]
    }
   ],
   "source": [
    "# coding=utf-8\n",
    "import sys\n",
    "import json\n",
    "\n",
    "IS_PY3 = sys.version_info.major == 3\n",
    "if IS_PY3:\n",
    "    from urllib.request import urlopen\n",
    "    from urllib.request import Request\n",
    "    from urllib.error import URLError\n",
    "    from urllib.parse import urlencode\n",
    "    from urllib.parse import quote_plus\n",
    "else:\n",
    "    import urllib2\n",
    "    from urllib import quote_plus\n",
    "    from urllib2 import urlopen\n",
    "    from urllib2 import Request\n",
    "    from urllib2 import URLError\n",
    "    from urllib import urlencode\n",
    "\n",
    "# API_KEY = '4E1BG9lTnlSeIf1NQFlrSq6h'\n",
    "# SECRET_KEY = '544ca4657ba8002e3dea3ac2f5fdd241'\n",
    "\n",
    "TEXT = \"欢迎使用百度语音合成。\"\n",
    "\n",
    "# 发音人选择, 基础音库：0为度小美，1为度小宇，3为度逍遥，4为度丫丫，\n",
    "# 精品音库：5为度小娇，103为度米朵，106为度博文，110为度小童，111为度小萌，默认为度小美 \n",
    "PER = 4\n",
    "# 语速，取值0-15，默认为5中语速\n",
    "SPD = 5\n",
    "# 音调，取值0-15，默认为5中语调\n",
    "PIT = 5\n",
    "# 音量，取值0-9，默认为5中音量\n",
    "VOL = 5\n",
    "# 下载的文件格式, 3：mp3(default) 4： pcm-16k 5： pcm-8k 6. wav\n",
    "AUE = 3\n",
    "\n",
    "FORMATS = {3: \"mp3\", 4: \"pcm\", 5: \"pcm\", 6: \"wav\"}\n",
    "FORMAT = FORMATS[AUE]\n",
    "\n",
    "CUID = \"123456PYTHON\"\n",
    "\n",
    "TTS_URL = 'http://tsn.baidu.com/text2audio'\n",
    "\n",
    "\n",
    "class DemoError(Exception):\n",
    "    pass\n",
    "\n",
    "\n",
    "\"\"\"  TOKEN start \"\"\"\n",
    "\n",
    "TOKEN_URL = 'http://aip.baidubce.com/oauth/2.0/token'\n",
    "SCOPE = 'audio_tts_post'  # 有此scope表示有tts能力，没有请在网页里勾选\n",
    "\n",
    "\n",
    "def fetch_token():\n",
    "    print(\"fetch token begin\")\n",
    "    params = {'grant_type': 'client_credentials',\n",
    "              'client_id': API_KEY,\n",
    "              'client_secret': SECRET_KEY}\n",
    "    post_data = urlencode(params)\n",
    "    if (IS_PY3):\n",
    "        post_data = post_data.encode('utf-8')\n",
    "    req = Request(TOKEN_URL, post_data)\n",
    "    try:\n",
    "        f = urlopen(req, timeout=5)\n",
    "        result_str = f.read()\n",
    "    except URLError as err:\n",
    "        print('token http response http code : ' + str(err.code))\n",
    "        result_str = err.read()\n",
    "    if (IS_PY3):\n",
    "        result_str = result_str.decode()\n",
    "\n",
    "    print(result_str)\n",
    "    result = json.loads(result_str)\n",
    "    print(result)\n",
    "    if ('access_token' in result.keys() and 'scope' in result.keys()):\n",
    "        if not SCOPE in result['scope'].split(' '):\n",
    "            raise DemoError('scope is not correct')\n",
    "        print('SUCCESS WITH TOKEN: %s ; EXPIRES IN SECONDS: %s' % (result['access_token'], result['expires_in']))\n",
    "        return result['access_token']\n",
    "    else:\n",
    "        raise DemoError('MAYBE API_KEY or SECRET_KEY not correct: access_token or scope not found in token response')\n",
    "\n",
    "\n",
    "\"\"\"  TOKEN end \"\"\"\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    token = fetch_token()\n",
    "    tex = quote_plus(TEXT)  # 此处TEXT需要两次urlencode\n",
    "    print(tex)\n",
    "    params = {'tok': token, 'tex': tex, 'per': PER, 'spd': SPD, 'pit': PIT, 'vol': VOL, 'aue': AUE, 'cuid': CUID,\n",
    "              'lan': 'zh', 'ctp': 1}  # lan ctp 固定参数\n",
    "\n",
    "    data = urlencode(params)\n",
    "    print('test on Web Browser' + TTS_URL + '?' + data)\n",
    "\n",
    "    req = Request(TTS_URL, data.encode('utf-8'))\n",
    "    has_error = False\n",
    "    try:\n",
    "        f = urlopen(req)\n",
    "        result_str = f.read()\n",
    "\n",
    "        headers = dict((name.lower(), value) for name, value in f.headers.items())\n",
    "\n",
    "        has_error = ('content-type' not in headers.keys() or headers['content-type'].find('audio/') < 0)\n",
    "    except  URLError as err:\n",
    "        print('asr http response http code : ' + str(err.code))\n",
    "        result_str = err.read()\n",
    "        has_error = True\n",
    "\n",
    "    save_file = \"error.txt\" if has_error else 'result.' + FORMAT\n",
    "    with open(save_file, 'wb') as of:\n",
    "        of.write(result_str)\n",
    "\n",
    "    if has_error:\n",
    "        if (IS_PY3):\n",
    "            result_str = str(result_str, 'utf-8')\n",
    "        print(\"tts api  error:\" + result_str)\n",
    "\n",
    "    print(\"result saved as :\" + save_file)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f090dbf9",
   "metadata": {},
   "source": [
    "## Jupyter播放音频文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "10285d33",
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'pydub'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-24-e0a537f8a045>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mpydub\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mAudioSegment\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mpydub\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mplayback\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mplay\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0msong\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mAudioSegment\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfrom_wav\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'error.txt'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'pydub'"
     ]
    }
   ],
   "source": [
    "from pydub import AudioSegment\n",
    "\n",
    "from pydub.playback import play\n",
    "\n",
    "song = AudioSegment.from_wav('error.txt')\n",
    "\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31f0fabe",
   "metadata": {},
   "source": [
    "## 智能机器人最终版"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "c7f60e6c",
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'pydub'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-25-fa7fc114e95d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 1. 录制音频\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mspeech_recognition\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mpydub\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mAudioSegment\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mpydub\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mplayback\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mplay\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'pydub'"
     ]
    }
   ],
   "source": [
    "# 1. 录制音频\n",
    "import speech_recognition\n",
    "from pydub import AudioSegment\n",
    "from pydub.playback import play\n",
    "\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone()as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\",\"wb\")as f:\n",
    "    f.write(audio.get_wav_data(convert_rate=16000))\n",
    "\n",
    "# 2.语音识别\n",
    "xf_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "\n",
    "# 3.图灵API\n",
    "TL_API(TL_KEY,asr_output)\n",
    "\n",
    "# 4.语音合成TTS --→ 产生(result.wav)\n",
    "tts(xf_token,TL_result)\n",
    "\n",
    "# 5 在Jupyter中进行音频播放\n",
    "song = AudioSegment.from_wav('error.txt')\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d4dd4175",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
